function N= func_wrapper_imputed(principal_data,teacher_data ,store_suffix, rc_on, N      )

%% this function computes output to explore the imuted obs


%% load principal prefs and clean them up

%rc_on=1
% this works: for a full string
folder='../../01_data/05_temp/';
fullmatfilename=fullfile(folder,principal_data);
load(fullmatfilename)

%load('../../../../ReplicateBuild/01_data/05_temp/mixed_logit_principal_totVA_imp')
 


   
    IJX_princ=[ ones(length(IJX_princ),1) IJX_princ];   % add a vector of ones  
    IJ_princ=IJX_princ * thetahat(1:end-1,1);     % don't include RC
    clearvars -except IJ_princ IJN* IJVA* principal_data teacher_data store_suffix N rc_on year

    
folder='../../01_data/05_temp/';
fullmatfilename=fullfile(folder, teacher_data);
load(fullmatfilename)
%load('../../../../ReplicateBuild/01_data/05_temp/mixed_logit_rc6_totVA_imp')


 
if rc_on==1
    % select the best Xdraws and clean them up
    [Xdraws_best] = func_likely_draws_current(IJitidx, IJchoiceset,IJcurrentFirst, Xdraws,Xmat,thetahat,y_logistic);
    
    % redo Xdraws_best to be as long as everything else
    [Ci, IAi, ICi]=unique(IJitidx(IJchoiceset==1));
    Xdraws_unique=Xdraws_best(IAi,:);
    Xdraws_best2=Xdraws_unique(IJitidx,:);
    
    % find predicted values in each match (teacher preferences)
    
    IJipref=[Xmat Xdraws_best2]*thetahat;
end

if rc_on==0
    
    IJipref=xb;
end
    
    % generate IJpref that is -999999999 if choiceset=0
    
    IJipref_choice=IJchoiceset.*IJipref + (1-IJchoiceset)*(-9999999999);
    
    % generate IJfracDisadv that is -99999999 if choicset=0
    
    IJfracDisadv_choice=IJchoiceset.*IJfracDisadv + (1-IJchoiceset)*(-9999999999);
 
    IJNdis= IJclassSize.*IJfracDisadv;
    IJNdis_choice=IJchoiceset.*IJNdis + (1-IJchoiceset)*(-9999999999);

    
    % build school preferences (by changing this block, can make it principal preferences, or....)
      
    IJoutput=IJclassSize.*IJVAma;
    IJoutput_dis=IJclassSize.*IJfracDisadv.*IJVAm2;
    IJoutput_adv=IJclassSize.*(1-IJfracDisadv).*IJVAm1;
    
  % generate ouput that is -999999999 if choiceset=0
    
    IJoutput_choice=IJchoiceset.*IJoutput + (1-IJchoiceset)*(-9999999999);   
    IJoutput_dis_choice=IJchoiceset.*IJoutput_dis + (1-IJchoiceset)*(-9999999999);   
    IJoutput_adv_choice=IJchoiceset.*IJoutput_adv + (1-IJchoiceset)*(-9999999999);   

    % generate IJ_princ that is -99999 if choiceset=0

    IJ_princ_choice=IJchoiceset.*IJ_princ + (1-IJchoiceset)*(-9999999999);  
    IJ_choice=(1-IJchoiceset)*(-9999999999);

    % generate principal for non-title I, and VA for title I
    
    IJ_princ_ntitleI_va_titleI=IJ_princ.*(IJfracDisadv<0.4) + IJoutput.*(IJfracDisadv>0.4)+IJoutput.*(IJfracDisadv==0.4);
    IJ_princ_titleI_va_ntitleI=IJ_princ.*(IJfracDisadv>0.4) + IJoutput.*(IJfracDisadv<0.4)+IJ_princ.*(IJfracDisadv==0.4);

    % generate 4 variables from the x matrix
    
    IJblack=Xmat(:,30);
    IJexp23=Xmat(:,33);
    IJexp46=Xmat(:,34);
    IJexp7p=Xmat(:,35);
    


%% 1. status quo, principal pref, restricted choice sets

% do this 2 times: balanced (makebalanced=1), schools short (makebalanced=0),
balanced_store=zeros(N,6);
school_short_store=zeros(N,6);



    for kk=1:N
       error_draws_princ=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both    
       error_draws_teach=evrnd(0,1,length(IJitidx),1); 
       
               
      balanced_store(kk,:)= func_allocations_imputed( IJipref+error_draws_teach,IJipref_choice+error_draws_teach,IJ_princ_choice+error_draws_princ, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJimputedteacher,IJappyear,2015,1,0);
      school_short_store(kk,:)= func_allocations_imputed( IJipref+error_draws_teach,IJipref_choice+error_draws_teach,IJ_princ_choice+error_draws_princ, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJimputedteacher,IJappyear,2015,0,0);
                  
    end 
    
  balanced=mean(balanced_store);
  school_short=mean(school_short_store);
    
base=['alloc_teach_est_princ_est_restr_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'balanced','school_short');
    
clearvars balanced school_short  balanced_store school_short_store 


%% 2. status quo, principal noisy, restricted choice sets

% do this 2 times: balanced (makebalanced=1), schools short (makebalanced=0),
balanced_store=zeros(N,6);
school_short_store=zeros(N,6);



    for kk=1:N
       error_draws_princ=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both    
       error_draws_teach=evrnd(0,1,length(IJitidx),1); 
       
               
      balanced_store(kk,:)= func_allocations_imputed( IJipref+error_draws_teach,IJipref_choice+error_draws_teach,IJ_choice+error_draws_princ, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJimputedteacher,IJappyear,2015,1,0);
      school_short_store(kk,:)= func_allocations_imputed( IJipref+error_draws_teach,IJipref_choice+error_draws_teach,IJ_choice+error_draws_princ, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJimputedteacher,IJappyear,2015,0,0);
                  
    end 
    
  balanced=mean(balanced_store);
  school_short=mean(school_short_store);
    
base=['alloc_teach_est_princ_noisy_restr_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'balanced','school_short');
    
clearvars balanced school_short  balanced_store school_short_store 

%% 3. status quo, principal pref, unrestricted choice sets

% do this 2 times: balanced (makebalanced=1), schools short (makebalanced=0),

balanced_store=zeros(N,6);
school_short_store=zeros(N,6);


    for kk=1:N

      error_draws_princ=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both    
      error_draws_teach=evrnd(0,1,length(IJitidx),1); 
              
      balanced_store(kk,:)= func_allocations_imputed( IJipref+error_draws_teach,IJipref+error_draws_teach,IJ_princ+error_draws_princ,   IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJimputedteacher,IJappyear,2015,1,0);
      school_short_store(kk,:)= func_allocations_imputed( IJipref+error_draws_teach,IJipref+error_draws_teach,IJ_princ+error_draws_princ, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJimputedteacher,IJappyear,2015,0,0);
     
    end 
    
    
  balanced=mean(balanced_store);
  school_short=mean(school_short_store);
    


base=['alloc_teach_est_princ_est_unrestr_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'balanced','school_short');
    
clearvars balanced school_short  balanced_store school_short_store 


%% 4. status quo, principal max VA, restricted choice sets

% do this 2 times: balanced (makebalanced=1), schools short (makebalanced=0),

balanced_store=zeros(N,6);
school_short_store=zeros(N,6);



    for kk=1:N

       error_draws_princ=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both    
       error_draws_teach=evrnd(0,1,length(IJitidx),1); 
       
               
      balanced_store(kk,:)= func_allocations_imputed( IJipref+error_draws_teach,IJipref_choice+error_draws_teach,IJoutput_choice, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJimputedteacher,IJappyear,2015,1,0);
      school_short_store(kk,:)= func_allocations_imputed( IJipref+error_draws_teach,IJipref_choice+error_draws_teach,IJoutput_choice, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJimputedteacher,IJappyear,2015,0,0);
                  
    end 
    
  balanced=mean(balanced_store);
  school_short=mean(school_short_store);
    
base=['alloc_teach_est_princ_va_restr_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'balanced','school_short');
    
clearvars balanced school_short  balanced_store school_short_store 



%% 5. status quo, principal pref, restricted choice sets, teahcer max Ndis

% do this 2 times: balanced (makebalanced=1), schools short (makebalanced=0),

balanced_store=zeros(N,6);
school_short_store=zeros(N,6);



    for kk=1:N

       error_draws_princ=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both    
       error_draws_teach=evrnd(0,1,length(IJitidx),1); 
       
               
      balanced_store(kk,:)= func_allocations_imputed( IJipref+error_draws_teach,IJNdis_choice,IJ_princ_choice+error_draws_princ,  IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJimputedteacher,IJappyear,2015,1,0);
      school_short_store(kk,:)= func_allocations_imputed( IJipref+error_draws_teach,IJNdis_choice,IJ_princ_choice+error_draws_princ,  IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJimputedteacher,IJappyear,2015,0,0);
           
       
    end 
    
  balanced=mean(balanced_store);
  school_short=mean(school_short_store);
    
base=['alloc_teach_ndis_princ_est_restr_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'balanced','school_short');
    
clearvars balanced school_short  balanced_store school_short_store 


%% 6. status quo, principal va, restricted choice sets, teahcer max Ndis

% do this 2 times: balanced (makebalanced=1), schools short (makebalanced=0),

balanced_store=zeros(N,6);
school_short_store=zeros(N,6);



    for kk=1:N

       error_draws_princ=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both    
       error_draws_teach=evrnd(0,1,length(IJitidx),1); 
       
               
      balanced_store(kk,:)= func_allocations_imputed( IJipref+error_draws_teach,IJNdis_choice,IJoutput_choice, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJimputedteacher,IJappyear,2015,1,0);
      school_short_store(kk,:)= func_allocations_imputed( IJipref+error_draws_teach,IJNdis_choice,IJoutput_choice, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJimputedteacher,IJappyear,2015,0,0);
           
       
    end 
    
  balanced=mean(balanced_store);
  school_short=mean(school_short_store);
    
base=['alloc_teach_ndis_princ_va_restr_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'balanced','school_short');
    
clearvars balanced school_short  balanced_store school_short_store 




%% 7. status quo, principal max VA for disadvantaged, unrestricted choice sets, also compute first best 

% do this 2 times: balanced (makebalanced=1), schools short (makebalanced=0),

balanced_store=zeros(N,6);
school_short_store=zeros(N,6);



    for kk=1:N

       error_draws_princ=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both    
       error_draws_teach=evrnd(0,1,length(IJitidx),1); 
       
               
      balanced_store(kk,:)= func_allocations_imputed( IJipref+error_draws_teach,IJipref+error_draws_teach,IJoutput_dis, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJimputedteacher,IJappyear,2015,1,1);
      school_short_store(kk,:)= func_allocations_imputed( IJipref+error_draws_teach,IJipref+error_draws_teach,IJoutput_dis, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJimputedteacher,IJappyear,2015,0,1);
                  
    end 
    
  balanced=mean(balanced_store);
  school_short=mean(school_short_store);
    
base=['alloc_teach_est_princ_va_unrestr_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'balanced','school_short');
    
clearvars balanced school_short  balanced_store school_short_store
  
end 



    
 